package com.yypinger.common.vaadin.springparser;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

import com.yypinger.common.misc.XmlLoader;
import com.yypinger.common.vaadin.TableColumnDefinition;

public class VaadinNamespaceHandler extends NamespaceHandlerSupport {

	@Override
	public void init() {
		//TableColumnDefinition
		registerBeanDefinitionParser("tcd", new VaadinTableColumnDefinitionParser());          
	}
	
	private static class VaadinTableColumnDefinitionParser extends AbstractSimpleBeanDefinitionParser{
		@Override
		protected Class getBeanClass(Element element) {
			return TableColumnDefinition.class;
		}

		@Override
		protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
			
			//parserContext.getDelegate().parseBeanDefinitionElement(element);
			
			String columnId = XmlLoader.getAttribute(element , "columnId" , null);
			builder.addPropertyValue("columnId", columnId);
			
			String columnHeader = XmlLoader.getAttribute(element , "columnHeader" , null);
			builder.addPropertyValue("columnHeader", columnHeader);
			
			
			String columnAlignment = XmlLoader.getAttribute(element , "columnAlignment" , null);
			builder.addPropertyValue("columnAlignment", columnAlignment);
			
			
			Class<?> columnType = XmlLoader.getAttributeAsClass(element, "columnType");
			builder.addPropertyValue("columnType", columnType);
			
			/*
			boolean isVisible = XmlLoader.getAttributeAsBoolean(element, "visible" , true);
			builder.addPropertyValue("visible", isVisible);
			*/
		}
	}

}
